#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
struct Peo
{
	char name[30];
	int id,year,month,day;
}pe[120];
bool cmpOld(const Peo& a, const Peo& b)
{
	if (a.year != b.year)
		return a.year < b.year;
	else if (a.month != b.month)
		return a.month < b.month;
	else if (b.day != a.day)
		return a.day < b.day;
	else
		return a.id > b.id;
}
int main() 
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		pe[i].id = i;
		scanf("%s%d%d%d", pe[i].name, &pe[i].year, &pe[i].month, &pe[i].day);
	}
	sort(pe, pe + n, cmpOld);
	for (int i = 0; i < n; i++)
	{
		printf("%s\n", pe[i].name);
	}
	return 0;
}